package com.fbs.wowo.userFollow.service.imp;

import com.fbs.wowo.essay.mapper.EssayMapper;
import com.fbs.wowo.essay.vo.EssayQuery;
import com.fbs.wowo.exception.DataOverflowException;
import com.fbs.wowo.exception.ErrorCode;
import com.fbs.wowo.manage.entity.User;
import com.fbs.wowo.manage.mapper.UserMapper;
import com.fbs.wowo.manage.service.UserService;
import com.fbs.wowo.message.entity.UserMsg;
import com.fbs.wowo.message.service.UserMsgService;
import com.fbs.wowo.message.util.MsgConst;
import com.fbs.wowo.userFollow.entity.FollowOrFansListResponse;
import com.fbs.wowo.userFollow.entity.UserFollow;
import com.fbs.wowo.userFollow.mapper.UserFollowMapper;
import com.fbs.wowo.userFollow.service.UserFollowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


@Service("UserFollowService")
@Transactional
public class UserFollowServiceImp implements UserFollowService {


    @Resource
    private UserFollowMapper userFollowMapper;

    @Resource
    private EssayMapper essayMapper;


    //添加关注
    @Override
    public int insertSelective(UserFollow record) {
        if (record.getUserId().equals(record.getFollowedUser())){
            throw new DataOverflowException(ErrorCode.ERR_FOLLOW_ONE);  //不能关注自己
        }
        UserFollow userFollow = userFollowMapper.selectByPrimaryKey(record.getUserId(), record.getFollowedUser());
        if (userFollow==null){
            record.setCreateDate(new Date());

            sendMsg(record.getFollowedUser(), record.getUserId());

            return userFollowMapper.insertSelective(record);
        }
        throw new DataOverflowException(ErrorCode.ERR_FOLLOW_TO);   //已关注
    }

    private void sendMsg(String userId, String enId) {
        User byUserId = userMapper.findByUserId(enId);
        UserMsg userMsg = new UserMsg();
        userMsg.setUserId(userId);
        userMsg.setMsgType(MsgConst.MsgFollow);
        userMsg.setMsgContent("有新的用户 " + byUserId.getName() + " 关注了你");
        userMsg.setEntityId(enId);
        userMsg.setEntityType(MsgConst.EnUser);
    }

    //查询数据详情
    @Override
    public UserFollow selectByPrimaryKey(String userId, String followedUser) {
        return userFollowMapper.selectByPrimaryKey(userId,followedUser);
    }

    //取消关注
    @Override
    public int deleteByPrimaryKey(String userId, String followedUser) {
        return userFollowMapper.deleteByPrimaryKey(userId,followedUser);
    }


    /**
     * 用户关注列表
     * @param userId
     * @return
     */
    @Override
    public List<FollowOrFansListResponse> findFollowList(String userId) {
        List<FollowOrFansListResponse> followList = userFollowMapper.findFollowList(userId);
        for (FollowOrFansListResponse follow: followList  ) {
            follow.setFansNum(userFollowMapper.findFollowNum(follow.getUserId()));
            EssayQuery essayQuery = new EssayQuery();
            essayQuery.setUserId(follow.getUserId());
            follow.setEssayNum(essayMapper.queryEssayList(essayQuery).size());
            follow.setFollowState(1);
        }
        return followList;
    }

    /**
     * 用户粉丝列表
     * @param userId
     * @return
     */
    @Override
    public List<FollowOrFansListResponse> findFansList(String userId) {
        List<FollowOrFansListResponse> fansList = userFollowMapper.findFansList(userId);
        for (FollowOrFansListResponse fans: fansList) {
            fans.setFansNum(userFollowMapper.findFansNum(fans.getUserId()));
            EssayQuery essayQuery = new EssayQuery();
            essayQuery.setUserId(fans.getUserId());
            fans.setEssayNum(essayMapper.queryEssayList(essayQuery).size());
            UserFollow userFollow = userFollowMapper.selectByPrimaryKey(userId, fans.getUserId());
            if (userFollow==null){
                fans.setFollowState(0);  //未关注
            }else{
                fans.setFollowState(1);  //已关注
            }

        }
        return fansList;
    }

    @Autowired
    private UserMsgService userMsgService;
    @Autowired
    private UserMapper userMapper;
}
